class Storeage {
  _store: Storage
  constructor(type: string) {
    this._store = (window as any)[`${type}Storage`]
  }

  has(key: string) {
    if (this._store.getItem(key)) {
      return true
    }
    return false
  }

  get(key: string, defaultValue: any = '') {
    if (this.has(key)) {
      return JSON.parse(this._store.getItem(key)!)
    }
    return defaultValue
  }

  set(key: string, value: any) {
    this._store.setItem(key, JSON.stringify(value))
    return this
  }

  remove(key: string) {
    if (this.has(key)) {
      this._store.removeItem(key)
    }
    return this
  }
}

// local/session
export default (type = 'session') => new Storeage(type)
